package com.freeman.cement.controller;

import com.alibaba.fastjson.JSONObject;
import com.freeman.cement.annotation.NeedRole;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.entity.WorkSection;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.mapper.WorksectionDao;
import com.freeman.cement.service.WorksectionService;
import com.freeman.cement.util.file.FileUploadUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//专业
@RestController
@RequestMapping("/worksection")
@CrossOrigin    //允许跨域访问
public class WorksectionController {

    @Resource
    private WorksectionService service;

    @Resource
    private WorksectionDao worksectionDao;

    /**
     * 分页查询专业列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectAllWorksectionByPage", method = RequestMethod.POST)
    public ResponseResult selectAllWorksectionByPage(@RequestParam int pageNo, @RequestParam int pageSize) {
        return service.selectWorksection(pageNo, pageSize);
    }

    /**
     * 新增专业
     * @param workSection
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/addWorkSection", method = RequestMethod.POST)
    public ResponseResult addWorkSection(WorkSection workSection) {
        return service.addWorksection(workSection);
    }

    /**
     * 删除专业
     * @param id
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/deleteWorkSection", method = RequestMethod.POST)
    public ResponseResult deleteWorkSection(@RequestParam("id") int id) {
        return service.deleteWorksection(id);
    }

    /**
     * 更新专业
     * @param workSection
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/updateWorkSection", method = RequestMethod.POST)
    public ResponseResult updateWorkSection(WorkSection workSection) {
        return service.updateWorksection(workSection);
    }

    /**
     * 项目图标上传
     * 使用“齐”的方式
     * 原理：
     * 1、上传图片到服务器指定文件夹，然后把图片的文件名或图片存储地址返回给前端
     * 2、前端新建项目提交时，把地址再返回给后端，后端将地址存入数据库
     *
     * @param file
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping("/uploadWorkSectionBanner")
    public ResponseResult uploadWorkSectionBanner(@RequestParam int worksectionId, @RequestParam MultipartFile file) {
        //工段判断
        if (!(worksectionId > 0)) {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
        WorkSection orgin = worksectionDao.selectById(worksectionId);
        if (orgin == null) {
            throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
        }

        // 判断文件是否为空，空则返回失败页面
        if (file.isEmpty()) {
            throw new AppException(AppExceptionCodeMsg.FILE_IS_NULL);
        } else {
            String fileNamePath = null;
            String orginalFileName = null;
            try {
                orginalFileName = file.getOriginalFilename();
                fileNamePath = FileUploadUtils.upload(file);
            } catch (IOException e) {
                throw new AppException(AppExceptionCodeMsg.UPLOAD_FILE_ERROR);
            }

            //然后把picPath路径值保存在数据库
            if (StringUtils.isNotBlank(fileNamePath)) {

                WorkSection worksection = new WorkSection();
                worksection.setId(orgin.getId());
                worksection.setIcon(fileNamePath);
                int rslt = worksectionDao.updateById(worksection);
                if (rslt > 0) {
                    Map<String, String> data = new HashMap<>();
                    data.put("orginalFileName", orginalFileName);
                    data.put("path", fileNamePath);
                    return ResponseResult.success("图标上传成功", data);
                } else {
                    throw new AppException(AppExceptionCodeMsg.UPDATE_WORKSECTION_ERROR);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.UPLOAD_FILE_ERROR);
            }

        }
    }

}
